package org.logsin37.tool.eurekamanager.api.controller;

import lombok.AllArgsConstructor;
import org.logsin37.tool.eurekamanager.domain.entity.FrozenConfig;
import org.logsin37.tool.eurekamanager.domain.service.FrozenDomainService;
import org.logsin37.tool.eurekamanager.infra.repository.FrozenConfigRepository;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Frozen Controller
 *
 * @author logsin37 2021-11-15
 */
@RestController
@RequestMapping("/eureka/manage/frozen")
@AllArgsConstructor
public class FrozenController {

    private FrozenConfigRepository frozenConfigRepository;
    private FrozenDomainService frozenDomainService;

    /**
     * 查询所有的冻结配置
     *
     * @return 所有的冻结配置, 只有code
     */
    @GetMapping("/config")
    public Flux<FrozenConfig> listFrozenConfig(){
        return this.frozenConfigRepository.listFrozenConfig();
    }

    /**
     * 根据eureka代码查询冻结配置
     *
     * @param eurekaCode eureka代码
     * @return 冻结配置
     */
    @GetMapping("/config/{eurekaCode}")
    public Mono<FrozenConfig> getConfigByEurekaCode(@PathVariable String eurekaCode){
        return this.frozenConfigRepository.getFrozenConfigByEurekaCode(eurekaCode);
    }

    /**
     * 保存冻结配置
     *
     * @param frozenConfig 冻结配置
     * @return 操作结果
     */
    @PostMapping("/config")
    public Mono<Void> saveFrozenConfig(@RequestBody FrozenConfig frozenConfig){
        return this.frozenConfigRepository.saveFrozenConfig(frozenConfig);
    }

    /**
     * 根据eureka代码移除冻结配置
     *
     * @param eurekaCode eureka代码
     * @return 操作结果
     */
    @DeleteMapping("/config/{eurekaCode}")
    public Mono<Void> removeFrozenConfigByCode(@PathVariable String eurekaCode){
        return this.frozenConfigRepository.removeFrozenConfigByEurekaCode(eurekaCode);
    }

    /**
     * 冻结
     *
     * @param eurekaCode eureka代码
     * @return 操作结果
     */
    @PostMapping("/{eurekaCode}")
    public Mono<Void> frozen(@PathVariable String eurekaCode){
        return this.frozenDomainService.frozen(eurekaCode);
    }

    /**
     * 解冻
     *
     * @param eurekaCode eureka代码
     * @return 操作结果
     */
    @DeleteMapping("/{eurekaCode}")
    public Mono<Void> unfrozen(@PathVariable String eurekaCode){
        return this.frozenDomainService.unfrozen(eurekaCode);
    }

}
